package com.chatplus.application.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.chatplus.application.common.constant.RedisPrefix;
import com.chatplus.application.dao.account.UserDao;
import com.chatplus.application.domain.entity.account.UserEntity;
import com.chatplus.application.domain.response.AccountSessionResponse;
import com.chatplus.application.service.account.UserService;
import com.chatplus.application.common.logging.SouthernQuietLogger;
import com.chatplus.application.common.logging.SouthernQuietLoggerFactory;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.codec.SerializationCodec;
import org.springframework.stereotype.Service;

/**
 * 用户表业务逻辑实现
 *
 * <p>Table: t_users - 用户表</p>
 *
 * @author developer
 * @see UserEntity
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    private static final SouthernQuietLogger LOGGER = SouthernQuietLoggerFactory.getLogger(UserServiceImpl.class);
    private final RedissonClient redissonClient;

    public UserServiceImpl(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public UserEntity getByUsername(String username) {
        return this.getOne(new LambdaQueryWrapper<>(UserEntity.class)
                .eq(UserEntity::getUsername, username).last("limit 1"));
    }

    @Override
    public boolean updateById(UserEntity entity) {
        redissonClient.getBucket(RedisPrefix.SESSION_PREFIX + entity.getId(), new SerializationCodec()).delete();
        return baseMapper.updateById(entity) > 0;
    }
}
